package org.doubango.FrCall.model;

import java.io.ByteArrayOutputStream;

import org.doubango.FrCall.Engine;
import org.doubango.FrCall.FrCall;
import org.doubango.FrCall.OpenfireNetService;
import org.doubango.FrCall.Screens.BaseScreen;
import org.doubango.FrCall.Services.Impl.OpenfireTask;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smackx.packet.VCard;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class User {
	final public static String VCARD_FIELD_SEX = "SEX";
	final public static String VCARD_FIELD_AGE = "AGE";
	final public static String VCARD_FIELD_REGION = "REGION";
	final public static String VCARD_FIELD_IDIOGRAPH = "IDIOGRAPH";
	final public static String VCARD_FIELD_LON = "LON";
	final public static String VCARD_FIELD_LAT = "LAT";
	final public static String VCARD_FIELD_ONLINE_STATUS = "ONLINE_STATUS";
	
	private String userId = "";	
	private String userName = "";
	private String nickName = "";
	private String NoteName = "";
	private Bitmap avatar = null;
	private String sex = "";
	private String age = "";
	private String address = "";
	private String city = "";
	private String provinces = "";
	private String country = "";
	private String email = "";
	private String idiograph = "";
	private Type onlineStatus =Presence.Type.unavailable;

	/** 经度 */
	private String lon = "";
	/** 维度 */
	private String lat = "";

	/** 完整的JID 带类似/Smack resource*/
	private String JID = "";	
	
	/** 是否有新消息 */
	private boolean hasNewMsg;
	/** 总消息数 */
	private int totalMsgNum;
	/** 新消息数 */
	private int newMsgNum;
	
	private VCard vCard;
	
	public User() {
		
	}
	public User(String userId,VCard vCard) {
		setUserId(userId);
		if(userId.contains("@"))
			userName = userId.substring(0, userId.indexOf("@"));
		else
			userName = userId;
		if(!userId.trim().contains(FrCall.s_MyUserID))
		{
			RosterEntry entry = BaseScreen.roster.getEntry(userId);
			if(entry!=null)
			{
				if(entry.getName() == null || (entry.getName() != null && entry.getName().trim().length()<=0))
				{
					NoteName = userName;
				}
				else
				{
					NoteName = entry.getName();
				}
			}
		}
		this.setvCard(vCard);
	}
	
	/** 保存*/
	public void save() {
		getvCard().setNickName(nickName);
		
		final ByteArrayOutputStream os = new ByteArrayOutputStream(); 
		if(avatar!=null)
		{
			avatar.compress(Bitmap.CompressFormat.PNG, 100, os);
			getvCard().setAvatar(os.toByteArray());
		}
		getvCard().setField(VCARD_FIELD_SEX, sex);
		getvCard().setField(VCARD_FIELD_AGE, age);
		getvCard().setAddressFieldHome(VCARD_FIELD_REGION, address);
		getvCard().setEmailHome(email);
		getvCard().setField(VCARD_FIELD_IDIOGRAPH, idiograph);
		getvCard().setField(VCARD_FIELD_LON, lon);
		getvCard().setField(VCARD_FIELD_LAT, lat);
		
		OpenfireNetService mOpenfireService = ((Engine)Engine.getInstance()).getOpenfireService();
		new OpenfireTask().execute(OpenfireTask.SAVE_VCARD,mOpenfireService,getvCard());
	}
	
	
	public String getLon() {
		return lon;
	}
	
	public void setLon(String lon) {
		this.lon = lon;
	}
	
	
	public String getLat() {
		return lat;
	}
	
	public void setLat(String lat) {
		this.lat = lat;
	}
	
	public int getTotalMsgNum() {
		return totalMsgNum;
	}
	
	public void setTotalMsgNum(int totalMsgNum) {
		this.totalMsgNum = totalMsgNum;
	}
	
	public int getNewMsgNum() {
		return newMsgNum;
	}
	
	public void setNewMsgNum(int newMsgNum) {
		this.newMsgNum = newMsgNum;
	}		
	
	public Boolean getHasNewMsg() {
		return hasNewMsg;
	}
	
	public void setHasNewMsg(boolean hasNewMsg) {
		this.hasNewMsg = hasNewMsg;
	}
	
	public void addMsg() {
		newMsgNum++;
		totalMsgNum++;
	}
	
	public String getJID() {
		return JID;
	}

	public void setJID(String JID) {
		this.JID = JID;
	}
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		if(userId.contains("/"))
			userId = userId.substring(0, userId.indexOf("/"));
		this.userId = userId;
		if (userName.trim().length() <= 0) {
			if (userId.contains("@"))
				userName = userId.substring(0, userId.indexOf("@"));
			else
				userName = userId;
		}
	}
	
	public String getDisplayName(){
		String displayName = userName;
		if(nickName !=null && nickName.length()>0)
			displayName=nickName;
		if(NoteName!=null&& NoteName.length()>0)
			displayName=NoteName;
		return displayName;
	}
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		if(userName!=null)
			this.userName = userName;
	}
	
	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		if(nickName!=null)
			this.nickName = nickName;
	}
	
	public Bitmap getAvatar() {
		return avatar;
	}

	public void setAvatar(Bitmap avatar) {
		this.avatar = avatar;
	}
	
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getEmail() {
		return email;
	}

	public void setaEmail(String email) {
		this.email = email;
	}
	
	public String getIdiograph() {
		return idiograph;
	}
	
	public void setIdiograph(String idiograph) {
		this.idiograph = idiograph;
	}
	public void setOnlineStatus(Type onlineStatus) {
		this.onlineStatus = onlineStatus;
	}
	public Type getOnlineStatus() {
		return onlineStatus;
	}
	public void setNoteName(String noteName) {
		if(NoteName !=null)
		NoteName = noteName;
	}
	public String getNoteName() {
		return NoteName;
	}
	@Override
	public String toString() {
		return getUserId();
	}
	public void setvCard(VCard vCard) {
		this.vCard = vCard;
		nickName = vCard.getNickName();
		if(nickName ==null)
			nickName ="";
		byte[] usericon_data = vCard.getAvatar();
		if(usericon_data!=null)
		{
			avatar = BitmapFactory.decodeByteArray(usericon_data, 0, usericon_data.length);
		}
		
		sex = vCard.getField(VCARD_FIELD_SEX);
		if(sex == null)
			sex ="未公开";
		if(sex.equals("male"))
			sex = "男";
		if(sex.equals("female"))
			sex = "女";
		age = vCard.getField(VCARD_FIELD_AGE);
		address = vCard.getAddressFieldHome(VCARD_FIELD_REGION);
		city = vCard.getAddressFieldHome("City");
		email = vCard.getEmailHome();
		idiograph = vCard.getField(VCARD_FIELD_IDIOGRAPH);
		lon = vCard.getField(VCARD_FIELD_LON);
		lat = vCard.getField(VCARD_FIELD_LAT);
	}
	public VCard getvCard() {
		return vCard;
	}
}
